<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Article;
use App\Http\Model\Category;
use App\Http\Model\Links;

class IndexController extends CommonController
{
	public function index()
	{
		//图文列表5篇（带分页）
		$data = Article::orderBy('article_time', 'desc')->paginate(5);

		//友情链接
		$links = Links::orderBy('link_order', 'asc')->get();
		return view('home.index', compact('data', 'links'));
    }

	public function cate($cate_id)
	{
		$field = Category::find($cate_id);
		//查看次数自增
		Category::where('cate_id', $cate_id)->increment('cate_view', 1);

		//图文列表5篇（带分页）
		$data = Article::where('cate_id', $cate_id)->orderBy('article_time', 'desc')->paginate(4);

		//当前分类的子分类
		$submenu = Category::where('cate_pid', $cate_id)->get();
		return view('home.list', compact('field', 'data', 'submenu'));
    }

	public function article($article_id)
	{
		//获取当前id的文章，使用关联查询获取分类信息
		$field = Article::Join('category', 'article.cate_id', '=', 'category.cate_id')->where('article_id', $article_id)->first();
		//查看次数自增
		Article::where('article_id', $article_id)->increment('article_view');
		//获取上一篇文章
		$article['pre'] = Article::where('article_id', '<', $article_id)->orderBy('article_id', 'desc')->first();
		//获取下一篇文章
		$article['next'] = Article::where('article_id', '>', $article_id)->orderBy('article_id', 'asc')->first();
		//获取相关文章，也就是cate_id一样的文章
		$other = Article::where('cate_id', $field->cate_id)->orderBy('article_id', 'desc')->take(6)->get();
		return view('home.new', compact('field', 'article', 'other'));
    }
}
